/*邮件管理*/
layui.define(['layer','form','layedit','element','upload'], function(exports) {
    var $ = layui.jquery;
    var layer=layui.layer;
    var form = layui.form();
    var layedit = layui.layedit;
    var element = layui.element();
    
    layedit.set({
        //编辑器内部上传图片
        uploadImage: {
            url: '/html/plan/srv/upd_plan_edit_img.php' //接口url
            ,ext: 'jpg|png|gif'
        }
    });
    //构建编辑器
    var index = layedit.build('content');

    //上传附件
    layui.upload({
        url: '/html/plan/srv/upd_plan_attachment.php/' //上传接口
        ,success: function(res){ //上传成功后的回调
            if($("#attachment").val()){
                var attachment=eval($("#attachment").val());
            }else{
                var attachment=[];
            }
            $.each(res,function(i,d){
                if(d.ret=='ok'){
                    var html='<span class="attachment_span"><a class="attachment_one" href="'+d.save_path+'">'+d.attachment_name+'</a><a class="attachment_one_del" data-id="'+d.attachment+'">[删除]</a></span>';
                    $("#attachment_box").append($(html));
                    var attach={'id':d.attachment,'name':d.attachment_name,'save_path':d.save_path};
                    attachment.push(attach);
                }
            })
            if(attachment.length>0)$("#attachment").val(JSON.stringify(attachment));

            $('.attachment_one_del').click(function(){
                var id = $(this).data('id');
                $(this).parent('.attachment_span').remove();
                var attachment=eval($("#attachment").val());
                $.each(attachment,function(index,item){
                    if(item.id==id){
                        attachment.splice(index,1);
                        return false;
                    }
                });
                $("#attachment").val(JSON.stringify(attachment));
            })     
        }
    });  

    $('.attachment_one_del').click(function(){
        var id = $(this).data('id');
        $(this).parent('.attachment_span').remove();
        var attachment=eval($("#attachment").val());
        $.each(attachment,function(index,item){
            if(item.id==id){
                attachment.splice(index,1);
                return false;
            }
        });
        $("#attachment").val(JSON.stringify(attachment));
    }) 

    $('#btn_save').click(function(){
        layedit.sync(index);
        if($("#type").val()==""){layer.msg("请选择计划类型");return false;}
        if($("#start_time").val()!=""&&$("#end_time").val()!=""&&$("#start_time").val()>$("#end_time").val())
        {layer.msg("开始时间不能大于结束时间");return false;}
        if($("#dept_id").val()==""){layer.msg("发布部门不能为空");return false;}
        if($("#accept_id").val()==""){layer.msg("发布人员不能为空");return false;}
        if($("#name").val()==""){layer.msg("请填写计划名称");return false;}
        if($("#content").val()==""){layer.msg("请填写计划内容");return false;}
        $.post('/html/plan/srv/edit_plan.php',$('#form1').serialize(),function(e){
            layer.msg(e);      
            setTimeout("parent.layer.close(parent.layer.getFrameIndex(window.name))", 2000);
        })
    });  
    exports('plan_edit', {});
    
});